View Javadoc
1 /*** 2 * Redistribution and use of this software and associated documentation 3 * ("Software"), with or without modification, are permitted provided 4 * that the following conditions are met: 5 * 6 * 1. Redistributions of source code must retain copyright 7 * statements and notices. Redistributions must also contain a 8 * copy of this document. 9 * 10 * 2. Redistributions in binary form must reproduce the 11 * above copyright notice, this list of conditions and the 12 * following disclaimer in the documentation and/or other 13 * materials provided with the distribution. 14 * 15 * 3. The name "Exolab" must not be used to endorse or promote 16 * products derived from this Software without prior written 17 * permission of Exoffice Technologies. For written permission, 18 * please contact tma@netspace.net.au. 19 * 20 * 4. Products derived from this Software may not be called "Exolab" 21 * nor may "Exolab" appear in their names without prior written 22 * permission of Exoffice Technologies. Exolab is a registered 23 * trademark of Exoffice Technologies. 24 * 25 * 5. Due credit should be given to the Exolab Project 26 * (http://www.exolab.org/). 27 * 28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS 29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 39 * OF THE POSSIBILITY OF SUCH DAMAGE. 40 * 41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved. 42 * 43 * $Id: IdentifierTest.java,v 1.6 2004/02/03 21:52:10 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.test.message.properties; 46 47 import java.util.Enumeration; 48 49 import javax.jms.ConnectionMetaData; 50 import javax.jms.JMSException; 51 import javax.jms.Message; 52 53 import junit.framework.Test; 54 55 import org.exolab.jmscts.core.AbstractMessageTestCase; 56 import org.exolab.jmscts.core.TestContext; 57 import org.exolab.jmscts.core.TestCreator; 58 import org.exolab.jmscts.test.message.util.PropertyValues; 59 60 61 /*** 62 * This class tests message property identifiers. 63 * 64 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 65 * @version $Revision: 1.6 $ 66 * @see AbstractMessageTestCase 67 */ 68 public class IdentifierTest extends AbstractMessageTestCase 69 implements PropertyValues { 70 71 /*** 72 * Valid identifiers 73 */ 74 private static final String[] VALID_IDENTIFIERS = { 75 "A", "_A", "$A", "a1", "_a1", "$a1", "a_a", "a$a", 76 "\u0041\u0061\u0391\u0430\u0301"}; 77 78 /*** 79 * Invalid identifiers 80 */ 81 private static final String[] INVALID_IDENTIFIERS = { 82 null, "1", "+1", "-1", "'1'", "a.b", "\u0030"}; 83 84 /*** 85 * Names reserved for message selectors. These are case-insensitive. 86 */ 87 private static final String[] RESERVED = { 88 "NULL", "TRUE", "FALSE", "NOT", "AND", "OR", "BETWEEN", "LIKE", "IN", 89 "IS", "ESCAPE"}; 90 91 /*** 92 * JMSX prefixed identifiers that may only be set by the provider 93 */ 94 private static final String[] JMSX_PROVIDER_IDENTIFIERS = { 95 "JMSXUserID", "JMSXAppID", "JMSXDeliveryCount", "JMSXProducerTXID", 96 "JMSXConsumerTXID", "JMSXRcvTimestamp", "JMSXState"}; 97 98 /*** 99 * Create an instance of this class for a specific test case, to be run 100 * against all message types 101 * 102 * @param name the name of test case 103 */ 104 public IdentifierTest(String name) { 105 super(name); 106 } 107 108 /*** 109 * Sets up the test suite 110 * 111 * @return an instance of this class that may be run by 112 * {@link org.exolab.jmscts.core.JMSTestRunner} 113 */ 114 public static Test suite() { 115 return TestCreator.createMessageTest(IdentifierTest.class); 116 } 117 118 /*** 119 * Verifies that valid identifiers may be used as property names 120 * 121 * @jmscts.requirement properties.identifier 122 * @throws Exception for any error 123 */ 124 public void testValidIdentifiers() throws Exception { 125 checkIdentifiers(VALID_IDENTIFIERS, false); 126 } 127 128 /*** 129 * Verifies that invalid identifiers can't be used as property names 130 * 131 * @jmscts.requirement properties.identifier 132 * @throws Exception for any error 133 */ 134 public void testInvalidIdentifiers() throws Exception { 135 checkIdentifiers(INVALID_IDENTIFIERS, true); 136 } 137 138 /*** 139 * Verifies that property names are case sensitive 140 * 141 * @jmscts.requirement properties.identifier.case 142 * @throws Exception for any error 143 */ 144 public void testIdentifierCase() throws Exception { 145 final String upper = "ABC"; 146 final String lower = upper.toLowerCase(); 147 TestContext context = getContext(); 148 Message message = context.getMessage(); 149 150 for (int i = 0; i < ALL_VALUES.length; ++i) { 151 for (int j = 0; j < ALL_VALUES[i].length; ++j) { 152 message.setObjectProperty(upper, ALL_VALUES[i][j]); 153 if (message.getObjectProperty(lower) != null) { 154 fail("getObjectProperty() is not treating names as being" 155 + " case sensitive"); 156 } 157 message.setObjectProperty(upper, null); 158 message.setObjectProperty(lower, ALL_VALUES[i][j]); 159 if (message.getObjectProperty(upper) != null) { 160 fail("getObjectProperty() is not treating names as being" 161 + " case sensitive"); 162 } 163 message.setObjectProperty(lower, null); 164 } 165 } 166 } 167 168 /*** 169 * Verifies that using a reserved word as a property name throws an 170 * exception. 171 * 172 * @jmscts.requirement properties.identifier.reserved 173 * @throws Exception for any error 174 */ 175 public void testReservedWords() throws Exception { 176 checkIdentifiers(RESERVED, true); 177 checkIdentifiers(toLowerCase(RESERVED), true); 178 } 179 180 /*** 181 * Tests that JMSX prefixed identifiers set by the provider cannot be 182 * set by clients. This should only apply to those identifiers that the 183 * provider supports 184 * 185 * @jmscts.requirement properties.provider 186 * @throws Exception for any error 187 */ 188 public void testJMSXProviderIdentifiers() throws Exception { 189 TestContext context = getContext(); 190 ConnectionMetaData metaData = context.getConnection().getMetaData(); 191 Enumeration iter = metaData.getJMSXPropertyNames(); 192 while (iter.hasMoreElements()) { 193 String name = (String) iter.nextElement(); 194 for (int i = 0; i < JMSX_PROVIDER_IDENTIFIERS.length; ++i) { 195 if (name.equals(JMSX_PROVIDER_IDENTIFIERS[i])) { 196 checkIdentifiers(new String[]{name}, true); 197 } 198 } 199 } 200 } 201 202 /*** 203 * Check that a list of identifiers can/can't be used as property names 204 * 205 * @param identifiers the list of identifiers 206 * @param fail if true, the test is expected to fail 207 * @throws Exception for any error 208 */ 209 private void checkIdentifiers(String[] identifiers, boolean fail) 210 throws Exception { 211 for (int i = 0; i < identifiers.length; ++i) { 212 String identifier = identifiers[i]; 213 for (int j = 0; j < ALL_VALUES.length; ++j) { 214 Object[] values = ALL_VALUES[j]; 215 for (int k = 0; k < values.length; ++k) { 216 Object value = values[k]; 217 if (fail) { 218 expectFailure(identifier, value); 219 } else { 220 expectSuccess(identifier, value); 221 } 222 } 223 } 224 } 225 } 226 227 /*** 228 * Expect the set property methods to succeed for a valid identifier 229 * 230 * @param identifier the valid identifier 231 * @param value the property value 232 * @throws Exception for any error 233 */ 234 private void expectSuccess(String identifier, Object value) 235 throws Exception { 236 237 Message message = getContext().getMessage(); 238 message.setObjectProperty(identifier, value); 239 PropertyHelper.setPrimitiveProperty(message, identifier, value); 240 } 241 242 /*** 243 * Expect the set property methods to fail for an invalid identifier 244 * 245 * @param identifier the invalid identifier 246 * @param value the property value 247 * @throws Exception for any error 248 */ 249 private void expectFailure(String identifier, Object value) 250 throws Exception { 251 String packagePrefix = "java.lang."; 252 253 Message message = getContext().getMessage(); 254 255 try { 256 PropertyHelper.setPrimitiveProperty(message, identifier, value); 257 String type = value.getClass().getName(); 258 if (type.startsWith(packagePrefix)) { 259 type = type.substring(packagePrefix.length()); 260 } 261 String method = "set" + type + "Property()"; 262 fail("Expected " + method + " to fail for invalid identifier=" 263 + identifier + ", value=" + value); 264 } catch (JMSException expected) { 265 // the expected behaviour 266 } 267 268 try { 269 message.setObjectProperty(identifier, value); 270 fail("Expected setObjectProperty() to fail for invalid " 271 + "identifier=" + identifier + ", value=" + value); 272 } catch (JMSException expected) { 273 // the expected behaviour 274 } 275 } 276 277 /*** 278 * Returns a lowercase version of an array of Strings 279 * 280 * @param strings a list of strings to convert to lowercase 281 * @return the corresponding array of lowercase strings 282 */ 283 private String[] toLowerCase(String[] strings) { 284 String[] result = new String[strings.length]; 285 for (int i = 0; i < strings.length; ++i) { 286 result[i] = strings[i].toLowerCase(); 287 } 288 return result; 289 } 290 291 }

This page was automatically generated by Maven